package qj.app.console;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.UnknownHostException;

import qj.util.ArrayUtil;
import qj.util.FileUtil;
import qj.util.ReflectUtil;
import qj.util.SystemUtil;
import qj.util.ThreadUtil;
import qj.util.funct.P0;

public class Exec {
	
	public static void main(String[] args) throws ClassNotFoundException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, UnknownHostException, IOException {

		final File commandFile = new File(SystemUtil.getCurrentDir() + "/wrapperCommands.txt");
		commandFile.delete();
		ThreadUtil.lowPriority(new P0() {public void e() {
			while (true) {
				ThreadUtil.sleep(800);
				if (commandFile.exists()) {
					String content = FileUtil.readFileToString(commandFile);
					commandFile.delete();
					for (String line : content.split("\r?\n")) {
						exec(line);
					}
				}
			}
		}});
		
//		ThreadUtil.run(new P0() {public void e() {
//			NetUtil.comingObjectLink(3123, new F1<Object, Object>() {public Object e(Object obj) {
//				F0<Object> f0 = (F0<Object>) obj;
//				return f0.e();
//			}});
//		}});
		
		ReflectUtil.getMethod("main", Class.forName(args[0]))
		.invoke(args, new Object[] { ArrayUtil.pop(args)});
	}
	
	private static void exec(String line) {
		System.out.println("Master command received: " + line);
		if ("exit".equals(line)) {
			System.exit(0);
		}
	}
}
